<?php

/**
 * Miffos framework
 * ----------------------------------------------
 *
 * Copyright (c) 2008, 2009 Martin Sadový (http://sodae.cz)
 *
 * This source file is subject to the "Miffos license" that is bundled
 * with this package in the file license.txt.
 *
 * @copyright  Copyright (c) 2008, 2009 Martin Sadový
 * @license    license.txt
 * @link       http://miffos.sodae.cz
 * @package    miffos
 * @version    $Id$
 */

 class FormRule{

    private $rule = array();

    private $control;

    public $error = array();

    private $form;

    public function __construct(Form $form){
        $this->form = $form;
    }

    /**
     * Add Rule
     * @param FormControl $control
     * @param string $rule
     * @param mixed  $data
     * @param string $massage
     */
    public function addRule(FormControl $control, $rule, $data, $massage)
    {
        $r = new stdClass;
        $r->rule    = $rule;
        $r->data    = $data;
        $r->massage = $massage;
        $r->control = $control;
        $this->rule[] = $r;
    }



    /**
     * Is Valide ?
     * @return bool
     */
    public function validation()
    {
        foreach($this->rule as $rule){
            if(!$this->valide($rule)){
                $this->error[] = $rule->massage;
                $false = true;
                if(!$this->form->autoRenderError()){
                    $this->form->addMessage($rule->massage);
                }
            }
        }
        
        return $false?false:true;
    }



    /**
     * Get error(s)
     * @return array
     */
    public function getError()
    {
        return $this->error;
    }



    /**
     * Validation rule(s)
     * @param object $r
     * @return bool
     */
    private function valide($r)
    {
        $value = $r->control->getValue();
        if ($r->data instanceof FormControl){
			$data = $r->data->getValue();
        }else{
            $data = $r->data;
        }
        
		if(ord($r->rule[0]) > 127){
		   $rule = ~$r->rule;
		   $negativ = true;
		}else{
           $rule = $r->rule;
		   $negativ = false;
		}
        switch ($rule) {
            case 'equal':
                $return = $value == $data;
                break;
            case 'filled':
                $return = !empty($value);
                break;
            case 'numeric':
                $return = is_numeric($value);
                break;
            case 'range':
                if (is_array($data) && count($data) == 2){
                    $return = in_array($value, range($data[0],$data[1]));
                }else{
                    $return = $value == $data;
                }
                break;
            case 'preg':
                $return = preg_match($data, $value);
                break;
            case 'email':
                $return = preg_match('/^[a-z0-9_\.-]{1,}@[a-z0-9\.-]{1,}$/', $value);
                break;
            case 'url':
                $return = preg_match('#^.+\.[a-z]{2,6}(\\/.*)?$#i', $value);
                break;
            case 'maxlength':
                $return = (strlen($value) <= $data);
                break;
            case 'minlength':
                $return = (strlen($value) >= $data);
                break;
            default:
                throw new Exception('Unknow rule:'.$rule);
                
		}
		return $negativ?!$return:$return;
    }
    
 }
?>